﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.Migrations;

namespace DynamicChart.Web.Models
{
    public class DynamicChartDbContext : DbContext
    {
        public DynamicChartDbContext()
            : base("name=" + SmartSpace.Configuration.DefaultConnectionStringName)
        {
            if (SmartSpace.Configuration.IsAutomaticMigrations)
            {
                Database.SetInitializer<DynamicChartDbContext>(new MigrateDatabaseToLatestVersion<DynamicChartDbContext, DBMC>());
            }
            else
            {
                Database.SetInitializer<DynamicChartDbContext>(null);
            }
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<InternalDataSource>().ToTable("Chart_InternalDataSource");
            modelBuilder.Entity<InternalDataSource>().HasKey(p => p.Name);
            base.OnModelCreating(modelBuilder);
        }
    }

    internal sealed class DBMC : DbMigrationsConfiguration<DynamicChartDbContext>
    {
        public DBMC()
        {
            AutomaticMigrationsEnabled = true;
            AutomaticMigrationDataLossAllowed = true;
        }
    }
}